<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<style>
    img {
        width: 100px;
        height: 80px;
        position: absolute;
        top: 2px;
    }
</style>

<body>
    <img src="./images/2.jpg" alt="">
    <script>
        //1、鼠标不断的移动，事件为mousemove
        //2、在页面中移动，给document注册事件
        //3、图片要移动距离，而且不占位置，使用绝对定位就行
        //4、核心原理：每次鼠标移动，都会获得鼠标的最新坐标，把这个x赋给图片的left,y赋给图片的top就可以

        var img = document.querySelector('img');
        document.addEventListener('mousemove', function (e) {
            // console.log(e.clientX);
            // console.log(e.clientY);
            var x = e.clientX;
            var y = e.clientY;
            //注意要给left和top添加单位才可以
            img.style.left = x -50 + 'px';
            img.style.top = y -40+ 'px'

        })

    </script>
</body>

</html>